/*
package com.gaofei.sysmanager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaofei.sysmanager.common.CommonResult;
import com.gaofei.sysmanager.domain.Menu;
import com.gaofei.sysmanager.domain.MenuRole;
import com.gaofei.sysmanager.service.IMenuRoleService;
import com.gaofei.sysmanager.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

*/
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gaofei
 * @since 2021-04-27
 *//*

@RestController
@RequestMapping("/menu")
@Slf4j//加上日志注解
public class MenuController {


    @Autowired
    IMenuService menuService;

    @Autowired
    IMenuRoleService menuRoleService;


    @RequestMapping("findMenuByUid")
    public List<Menu> findMenuByUid (Integer uid){
        log.info("dayin:{}",uid);
        //根据用户id查询一级菜单
        List<Menu> oneL = menuService.findMenusByUid(uid,1);
        System.out.println("^^^^^"+oneL);
        return selectChild(uid,oneL);
    }

    private List<Menu> selectChild(Integer uid,List<Menu> oneL) {
        List<Menu> oneList = new ArrayList<>();

        //遍历一级菜单
        if(oneL!=null&& oneL.size()>0){
            oneL.forEach(oneMenu -> {
                System.out.println(oneMenu.getName()+"****");
                //根据一级的id作为二级的parentId查询二级菜单
                List<Menu> twoL = menuService.findMenusByUid(uid,oneMenu.getId());
//                多个角色有相同的二级菜单去重
                twoL=twoL.stream().distinct().collect(Collectors.toList());
                if(twoL!=null&& twoL.size()>0){
                    oneMenu.setChildren(twoL);
                    //递归调用
                    selectChild(uid,twoL);
                }
                oneList.add(oneMenu);
            });
        }
        //去重  多个角色有相同的一级菜单去重
        List<Menu> collect = oneList.stream().distinct().collect(Collectors.toList());
        return collect;
    }


    */
/*@Autowired
    private RedisTemplate redisTemplate;*//*



    */
/**
     *
     * 需要两种数据
     * 1.所有的树的数据
     * 2.需要回显的数据-->根据roleId查询的菜单id数组:菜单的level!=1的
     * @param roleId
     * @return
     *//*

    @RequestMapping("getTree")
    public CommonResult getTree(Integer roleId){
        HashMap<Object, Object> map = new HashMap<>();
        List<Menu> menuList =menuService.getTree(0);

       */
/* //使用redis来缓存加载的树
       List<Menu> menuList= null;//查询所有的菜单
        if(redisTemplate.hasKey("menuTree")){
            menuList= redisTemplate.opsForList().range("menuTree", 0,-1);
            log.info("从redis中取的tree*****************");
        }else {
            //从数据库中查询
            menuList =menuService.getTree(0);
            //存入redis
            redisTemplate.opsForList().leftPushAll("menuTree",menuList);
            log.info("从mysql中取的tree,并存入了redis*****************");
        }*//*






        QueryWrapper wrapper = new QueryWrapper();
        QueryWrapper ww = new QueryWrapper();
        wrapper.eq("rid",roleId);
        //根据角色id查询角色对应的菜单
        List<MenuRole> list = menuRoleService.list(wrapper);


        */
/*ArrayList<Object> list1 = new ArrayList<>();
        for (MenuRole menuRole : list) {
            Integer mid = menuRole.getMid();
            list1.add(mid);
        }*//*


        //map方法和filter方法的区别map不改变原始集合的大小,filter是改变的
        //通过stream流的方式,从list中过滤获取菜单id的集合
        List<Integer> mids = list.stream().map((mr) -> mr.getMid()).collect(Collectors.toList());//jdk8之后的新特性:stream流

        //根据菜单id的数组查询菜单集合
        List<Menu> menus = menuService.findMenusByIds(mids);

        //过滤掉菜单的level!=1的菜单,如果不过滤掉的话,就会选中顶级菜单,导致所有子菜单都会被选中
        List<Menu> collect1 = menus.stream().filter(menu -> menu.getLevel() != 1).collect(Collectors.toList());
        //获取到选中的id  ,且返回数据
        List<Integer> collect = collect1.stream().map(menu -> menu.getId()).collect(Collectors.toList());
        System.out.println(collect+"======================================================");
        map.put("mids", collect); //需要回显的id
        map.put("menuList", menuList);//所有的菜单
        return CommonResult.success(map);

    }
    @RequestMapping("saveMenuRole")
    public boolean save(@RequestBody Map<String,Object> map){
        try {

            System.err.println(map+"%%%%%%%%%%");
            Integer rid = (Integer) map.get("rid");
            ArrayList<Integer> mids = (ArrayList<Integer>) map.get("mids");//从map中获取参数


            //先删除根据rid  menu_role表
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("rid", rid);

            menuRoleService.remove(wrapper);
            //再加中间表

            MenuRole menuRole = new MenuRole();
            for (Integer mid : mids) {
                if(mid!=1){//保存的时候,去掉顶级节点
                    menuRole.setMid(mid);
                    menuRole.setRid(rid);
                    menuRoleService.save(menuRole);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    @GetMapping(path = "sendEmail1")
    public void setMsgUtil11() throws Exception {

        Properties props = new Properties();
        props.put("mail.smtp.host ", "smtp.163.com ");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);
        Message message = new MimeMessage(session);
        InternetAddress from = new InternetAddress("junjie0608_w@163.com");
        from.setPersonal(MimeUtility.encodeText("俊杰"));
        message.setFrom(from);
        InternetAddress to = new InternetAddress("15631382687@163.com");
        message.setRecipient(Message.RecipientType.TO, to);
        message.setSubject(MimeUtility.encodeText("反馈标题"));
        message.setSentDate(new Date());
        MimeMultipart msgMultipart = new MimeMultipart("mixed");// 指定为混合关系
        message.setContent(msgMultipart);

        String s = readFile("C:\\Users\\93173\\Documents\\WeChat Files\\wxid_7nnid3kbw9lc12\\FileStorage\\File\\2022-04\\index.html");


        // 邮件内容
        MimeBodyPart htmlPart = new MimeBodyPart();
//        htmlPart.setContent(
//                "<body background='http://dl.iteye.com/upload/picture/pic/110267/e244bda9-9034-36e3-87fd-807629b84222.jpg'>"
//                        + "<div style='position: absolute; left: 390px; top: 150px;height: "
//                        + "100px;width: 200px;' align='center'>"
//                        + "<font color='red'>这是测试邮件，请勿回复</font>" + "</div></body>",
//                "text/html;charset=UTF-8");

        htmlPart.setContent(
                s,
                "text/html;charset=UTF-8");
        /*htmlPart.setContent("反馈内容", "text/html;charset=UTF-8");*/
/*

// TODO 组装的顺序非常重要，一定要先组装文本域，再组装文件

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;msgMultipart.addBodyPart(htmlPart);
        // 组装附件
       /* MimeBodyPart file = new MimeBodyPart();
        FileDataSource file_datasource = new FileDataSource(urlFile);
        DataHandler dh = new DataHandler(file_datasource);
        file.setDataHandler(dh);*/
/*
        // 附件区别内嵌内容的一个特点是有文件名，为防止中文乱码要编码
//        file.setFileName(MimeUtility.encodeText(dh.getName()));
//        msgMultipart.addBodyPart(file);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.163.com", 25, "junjie0608_w@163.com", "KASNCVNHBGJOXSHQ");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        System.out.println("发送完毕");

        }


@GetMapping(path = "asdasdasd")
public void sendAttachmentMail(String to, String title, String content, String path) {
        path = "C:\\Users\\93173\\Documents\\WeChat Files\\wxid_7nnid3kbw9lc12\\FileStorage\\File\\2022-04\\Html.txt";
        System.out.println(to+title+content+path);
        try {
        MultiPartEmail mail = new MultiPartEmail();
        // 设置端口
        mail.setSmtpPort(port);
        // 设置邮件服务器地址
        mail.setHostName(host);
        // 设置密码验证
        mail.setAuthentication(from, password);
        // 邮件发送者
        mail.setFrom(from);
        // 邮件接收者
        mail.addTo(to);
        // 邮件编码
        mail.setCharset("UTF-8");
        // 邮件主题
        mail.setSubject(title);
        //邮件内容
        mail.setMsg(content);
        // 创建附件
        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath(path);
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        mail.attach(attachment);
        // 设置邮件发送时间
        mail.setSentDate(new Date());
        // 发送邮件
        mail.send();
        } catch (Exception e) {
        throw new RuntimeException(e);
        }
        }


@PostMapping(path = "getFileUrl")
public String upload(MultipartFile uploadFile, HttpServletRequest req) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
        // 上传的文件将保存在项目运行目录下的 uploadFile 文件夹，
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
        System.out.println(realPath);

        // 并且在 uploadFile 文件夹中通过日期对上传的文件归类保存
        // 比如：/uploadFile/2019/06/06/32091e5f-c9e9-4506-9567-43e724f1fe37.png
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if (!folder.isDirectory()) {
        folder.mkdirs();
        }

        // 对上传的文件重命名，避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString()
        + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
        // 文件保存
        uploadFile.transferTo(new File(folder, newName));

        // 返回上传文件的访问路径
        String filePath = req.getScheme() + "://" + req.getServerName()
        + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
        urlFile = filePath;
        return filePath;
        } catch (IOException e) {
        e.printStackTrace();
        }
        return "上传失败!";
        }


public String readFile( String filePath ){
        FileInputStream fis=null;
        String result = "" ;
        try {
        // 根据path路径实例化一个输入流的对象
        fis  = new FileInputStream( filePath );

        //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值；
        int size =  fis.available() ;
        //3. 根据输入流中的字节数创建byte数组；
        byte[] array = new byte[size];
        //4.把数据读取到数组中；
        fis.read( array ) ;

        //5.根据获取到的Byte数组新建一个字符串，然后输出；
        result = new String(array);

        } catch (FileNotFoundException e) {
        e.printStackTrace();
        }catch (IOException e) {
        e.printStackTrace();
        }finally{
        if ( fis != null) {
        try {
        fis.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }
        }

        return result ;
}

*/
